﻿@namespace Bit.BlazorUI
@inherits BitComponentBase

@{
    var hasPrimary = (PrimaryTemplate ?? ChildContent) is not null;
    var hasSecondary = SecondaryText.HasValue() || SecondaryTemplate is not null;
}

@if (Href.HasNoValue())
{
    <button @ref="RootElement" @attributes="HtmlAttributes" @onclick="HandleOnClick"
            id="@_Id"
            style="@StyleBuilder.Value"
            class="@ClassBuilder.Value"
            type="@_buttonType.GetValue()"
            dir="@Dir?.ToString().ToLower()"
            disabled="@(IsEnabled is false)"
            title="@Title"
            tabindex="@_tabIndex"
            aria-label="@AriaLabel"
            aria-hidden="@AriaHidden"
            aria-describedby="@AriaDescription">
        @if (IsLoading is false)
        {
            @if (IconName.HasValue())
            {
                <i style="@Styles?.Icon" class="bit-btn-icn bit-icon bit-icon--@IconName @Classes?.Icon" />
            }
            else if (IconUrl.HasValue())
            {
                <img src="@IconUrl" style="@Styles?.Icon" class="bit-btn-icnu @Classes?.Icon" />
            }

            @if (IconOnly is false && (hasPrimary || hasSecondary))
            {
                <div style="@Styles?.Container" class="bit-btn-tcn @Classes?.Container">
                    @if (hasPrimary)
                    {
                        <div style="@Styles?.Primary" class="bit-btn-prt @Classes?.Primary">
                            @(PrimaryTemplate ?? ChildContent)
                        </div>
                    }

                    @if (hasSecondary)
                    {
                        <div style="@Styles?.Secondary" class="bit-btn-sct @Classes?.Secondary">
                            @if (SecondaryTemplate is not null)
                            {
                                @SecondaryTemplate
                            }
                            else
                            {
                                @SecondaryText
                            }
                        </div>
                    }
                </div>
            }
        }
        else
        {
            if (LoadingTemplate is null)
            {
                <div style="@Styles?.LoadingContainer" class="bit-btn-ldg @GetLabelPositionClass() @Classes?.LoadingContainer">
                    <div style="@Styles?.Spinner" class="bit-btn-spn @Classes?.Spinner"></div>
                    @if (LoadingLabel.HasValue())
                    {
                        <div style="@Styles?.LoadingLabel" class="bit-btn-lbl @Classes?.LoadingLabel">
                            @LoadingLabel
                        </div>
                    }
                </div>
            }
            else
            {
                @LoadingTemplate
            }
        }
    </button>
}
else
{
    <a @ref="RootElement" @attributes="HtmlAttributes"
       id="@_Id"
       rel="@_rel"
       style="@StyleBuilder.Value"
       class="@ClassBuilder.Value"
       dir="@Dir?.ToString().ToLower()"
       disabled="@(IsEnabled is false)"
       href="@(IsEnabled ? Href : null)"
       title="@Title"
       target="@Target"
       tabindex="@_tabIndex"
       aria-label="@AriaLabel"
       aria-hidden="@AriaHidden"
       aria-describedby="@AriaDescription">
        @if (IconName.HasValue())
        {
            <i style="@Styles?.Icon" class="bit-btn-icn bit-icon bit-icon--@IconName @Classes?.Icon" />
        }
        else if (IconUrl.HasValue())
        {
            <img src="@IconUrl" style="@Styles?.Icon" class="bit-btn-icnu @Classes?.Icon" />
        }

        @if (IconOnly is false && (hasPrimary || hasSecondary))
        {
            <div style="@Styles?.Container" class="bit-btn-tcn @Classes?.Container">
                @if (hasPrimary)
                {
                    <div style="@Styles?.Primary" class="bit-btn-prt @Classes?.Primary">
                        @(PrimaryTemplate ?? ChildContent)
                    </div>
                }

                @if (hasSecondary)
                {
                    <div style="@Styles?.Secondary" class="bit-btn-sct @Classes?.Secondary">
                        @if (SecondaryTemplate is not null)
                        {
                            @SecondaryTemplate
                        }
                        else
                        {
                            @SecondaryText
                        }
                    </div>
                }
            </div>
        }
    </a>
}
